package com.msmall.community.klg.util;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.Constants;
import com.msmall.base.util.DateUtil;
import com.msmall.base.util.FileIO;
import com.msmall.base.util.StringUtil;
import com.msmall.community.common.Configuration;

public class KLGUtil {
	private static final Log logger = LogFactory.getLog(KLGUtil.class);
	
	public static final String PREFIX_WML = "file/wml/klg/";
	public static final String PREFIX_HTML = "file/html/klg/";
	

	/**
	 * 获文章标题最少字符数
	 * @return
	 */
	
	public static int getArticleTitleMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_ARTICLE_TITLE_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得文章标题最多字符数
	 * @return
	 */
	public static int getArticleTitleMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_ARTICLE_TITLE_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	/**
	 * 获得文章内容最少字符数
	 * @return
	 */
	public static int getArticleContentMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_ARTICLE_CONTENT_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得文章内容最多字符数
	 * @return
	 */
	public static int getArticleContentMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_ARTICLE_CONTENT_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	/**
	 * 文章内容摘要长度
	 * @return
	 */
	public static int getContentAbstractChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_ARTICLE_CONTENT_ABSTRACT_CHARS));
		}catch(Exception e){
			return 500;
		}
	}
	
	/**
	 * 判断文章标题字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkArticleTitleLength(String title){
		if( StringUtil.nullOrBlank(title))
			return 2;
		int length = title.length();
		if(getArticleTitleMinChars() > 0 ){
			if(length < getArticleTitleMinChars())
				return 2;
		}
		if(getArticleTitleMaxChars() > 0 ){
			if( length > getArticleTitleMaxChars())
				return 3;
		}
		return 1;
	}
	/**
	 * 判断文章内容字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkArticleContentLength(String content){
		if(StringUtil.nullOrBlank(content) )
			return 2;
		int length = content.length();
		if(getArticleContentMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getArticleContentMinChars())
				return 2;
		}
		if(getArticleContentMaxChars() > 0 ){
			if(length > getArticleContentMaxChars())
				return 3;
		}
		return 1;
	}
	
	/**
	 * 获得文章回复内容最少字符数
	 * @return
	 */
	public static int getArticleReplyMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_ARTICLE_REPLY_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得文章回复内容最多字符数
	 * @return
	 */
	public static int getArticleReplyMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_ARTICLE_REPLY_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}

	/**
	 * 判断文章回复内容字符数是否在最大或最小之间
	 * @param title
	 * @return  2:字符数太少，3字符数太多，1正常
	 */
	public static int checkArticleReplyLength(String content){
		if(StringUtil.nullOrBlank(content))
			return 2;
		int length = content.length();
		if(getArticleReplyMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getArticleReplyMinChars())
				return 2;
		}
		if(getArticleReplyMaxChars() > 0 ){
			if(length > getArticleReplyMaxChars())
				return 3;
		}
		return 1;
	}
	
	public static String readArticleFile(String filePath){
		return FileIO.readFile(Constants.ROOTPATH + filePath, Constants.DEFAULT_CHARSET);
	}
	/**
	 * @return 0=filePath; 1=fileName; 2= fileExt(文件扩展名)
	 * @param title 标题
	 * @param content 内容
	 * @param entityId 实体ID，TopicID or ThreadID
	 * @param date 创建日期
	 * @throws IOException 
	 *
	 */
	public static String[] writeArticlemFile(String content,Date date, String uat) throws IOException{
		String filePath = getArticleFolderPath(date);
		String fileName = getArticleFileName();
		String path = Constants.ROOTPATH + getPrefixPath(uat) + filePath + fileName + Constants.FILE_HTML_EXTENDS;
		FileIO.writeFile(content, path);
		return new String[]{filePath, fileName, Constants.FILE_HTML_EXTENDS};
	}
	
	public static void updateArticleFile(String content, String filePath, String fileName, String fileExt, String uat) throws IOException{
		FileIO.writeFile(content, Constants.ROOTPATH + getPrefixPath(uat) + filePath + fileName + fileExt);
	}
	
	public static void deleteArticleFile(String filePath, String fileName, String uat){
		FileIO.deleteFile(Constants.ROOTPATH + getPrefixPath(uat)  + filePath + fileName);
	}
	
	public static String getArticleFileName(){
		return FileIO.getRandomFileName();
	}
	/**
	 * 生成Article存储路径 date/hours/minute%10/
	 * @param date 创建时间
	 */
	public static String getArticleFolderPath(Date date){
		String folderPath = "";
		StringBuilder builder = new StringBuilder();
		builder.append(DateUtil.formatDateTime(date, "yyyyMMdd"))
		.append(Constants.FILE_SEP)
		.append(DateUtil.getHour(date))
		.append(Constants.FILE_SEP)
		.append(DateUtil.getMinute(date)%10)
		.append(Constants.FILE_SEP);
		
		folderPath = builder.toString();
		File folder = new File(folderPath);
		if(!folder.exists())
			folder.mkdirs();
		return folderPath;
	}
	
	public static String getPrefixPath(String uat){
		if(Constants.USER_AGENT_TYPE_HTML.equals(uat)){
			return PREFIX_HTML;
		}
		if(Constants.USER_AGENT_TYPE_WML.equals(uat)){
			return PREFIX_WML;
		}
		return PREFIX_WML;
	}
}
